package com.jdbc;


import java.sql.*;

/*
mysql数据库连接
 */
public class JdbcConnection {
    //数据库url、用户、密码
    static final String DB_URL = "jdbc:mysql://106.52.210.59:3306/mydb?";
    static final String USER = "root";
    static final String PSW = "78.432bCF0123qqc";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载jdbc驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取数据库连接
        Connection connection= DriverManager.getConnection(DB_URL,USER,PSW);
        //3.操作数据库
        Statement statement= connection.createStatement(); //获取操作数据库的对象
        String sql="insert into user(username,password) values('2022','2022')";
        boolean execute = statement.execute(sql);
        System.out.println(execute);
//        ResultSet resultSet= statement.executeQuery(sql); //执行sql语句，获取结果集

//        while (resultSet.next()){
//            int id=resultSet.getInt("id");
//            String username=resultSet.getString("username");
//            String password=resultSet.getString("password");
//
//            System.out.println("人数"+id);
//            System.out.println("用户名"+username);
//            System.out.println("密码"+password);
//        }
        //4.关闭结果集、数据库操作对象、数据库连接
//        resultSet.close();
        statement.close();
        connection.close();
    }
}
